package pl.plesken.swt;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import pl.plesken.languages.SetLanguage;
import pl.plesken.tools.MovieJukeBoxConfig;

public class Properties extends Dialog {

	private StyledText propertiesStyledText;
	protected Object result;
	protected Shell shell;
	StringBuffer confTemp = new StringBuffer();
	StringBuffer confExp = new StringBuffer();

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public Properties(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public Properties(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		MovieJukeBoxConfig load = new MovieJukeBoxConfig();
		
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		final GridLayout gridLayout = new GridLayout();
		shell.setLayout(gridLayout);
		shell.setSize(673, 543);
		shell.setText(SetLanguage.getString("Properties.AppName")); //$NON-NLS-1$

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		propertiesStyledText = new StyledText(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		final GridData gd_propertiesStyledText = new GridData(SWT.FILL, SWT.FILL, true, true);
		propertiesStyledText.setLayoutData(gd_propertiesStyledText);
		
		load.loadTempConf();
		propertiesStyledText.setText(load.getConfTemp().toString());

		final Menu menu = new Menu(propertiesStyledText);
		propertiesStyledText.setMenu(menu);

		final MenuItem searchMenuItem = new MenuItem(menu, SWT.NONE);
		searchMenuItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				search();
			}
		});
		searchMenuItem.setText("Znajdź...");

		final Button setButton = new Button(shell, SWT.NONE);
		setButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				setIt();
			}
		});
		final GridData gd_setButton = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		gd_setButton.widthHint = 80;
		setButton.setLayoutData(gd_setButton);
		setButton.setText(SetLanguage.getString("Properties.Accept")); //$NON-NLS-1$
		//
	}

	private void search() {
		Search s = new Search(shell);
		try {
			FileInputStream fin = new FileInputStream("conf/moviejukebox.temp");
			BufferedReader in = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
		
			s.open();
			String word = s.getWord();
			System.out.println("wielkość: " + propertiesStyledText.getText().length());
			for (int i = 0; i < propertiesStyledText.getText().length(); i++) {
				System.out.println("Linia: " + propertiesStyledText.getText(0, i));
				if (propertiesStyledText.getText(i, i).contains(word)) {
					propertiesStyledText.setBackground(new Color(Display.getDefault(), 127, 178, 127));
				}
			}
			/*while ((line = in.readLine()) != null) {
				if (line.contains(word)) {
					line.;
				}
			}*/
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		} catch(UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch(NullPointerException e) {
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	private void setIt() {
		MovieJukeBoxConfig export = new MovieJukeBoxConfig();
		
		confExp.append(propertiesStyledText.getText());
		export.setConfTemp(confExp);
		export.saveTempConf();
		
		shell.close();
	}
	
}
